﻿using Clothes.Comment;
using Clothes.DAL;
using Clothes.IBLL;
using Clothes.IDAL;
using Clothes.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Clothes.BLL
{
    public class CLLoginBLL : CLLoginIBLL
    {
        CLLoginIDAL Login = new CLLoginDAL();
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="Number">手机号</param>
        /// <param name="Password">密码</param>
        /// <param name="Manager">是否是管理员（0不是 1是）</param>
        /// <returns></returns>
        public OperResult Register(string name, string Number, string Password, int Manager)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return OperResult.Failed("用户名不能为空");
            }
            if (string.IsNullOrWhiteSpace(Number))
            {
                return OperResult.Failed("手机号不能为空");
            }
            if (string.IsNullOrWhiteSpace(Password))
            {
                return OperResult.Failed("密码不能为空");
            }
            
            if (Login.LoginName(name))
            {
                return OperResult.Failed("用户名已存在");
            }
            if (Login.LoginNumber(Number))
            {
                return OperResult.Failed("手机号已存在");
            }
            Encryption encryption = new Encryption();
            Model.CLLogin login = new Model.CLLogin()
            {
                UserName = name,
                MobileNumber = Number,
                Password = encryption.SHA1Encryp(Password),
                Manager = Manager,
                Delect = false
            };
            if (Login.Insert(login))
            {
                return OperResult.Success("添加成功");
            }
            else
            {
                return OperResult.Failed("添加失败");
            }

        }

        SHAEncryption shaEncryption = new SHAEncryption();
        /// <summary>
        /// 根据手机号和密码查找信息
        /// </summary>
        /// <param name="mobileNumber"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public CLLogin SelectBLL(string mobileNumber, string password)
        {
            return Login.Select(mobileNumber, shaEncryption.SHA1Encrypt(password));
        }

        /// <summary>
        /// 根据姓名和密码查找信息
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public CLLogin SelectSourceBLL(string UserName, string password)
        {
            return Login.SelectSource(UserName, shaEncryption.SHA1Encrypt(password));
        }

        public CLLogin SelectId(int id)
        {
            return Login.Tiao(a=>a.Id==id);
        }

    }
}
